package svc

import (
	"pusher/internal/config"
	"time"

	"github.com/segmentio/kafka-go"
	"github.com/zeromicro/go-queue/kq"
)

const (
	TIME_FORMAT = "2006-01-02 15:04:05"
)

type ServiceContext struct {
	Config       config.Config
	TimeFormat   string
	KafkaPushCli *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	pusher := kq.NewPusher(
		c.KqPusherConf.Brokers,
		c.KqPusherConf.Topic,
		// 消息体达到大小才提交
		kq.WithChunkSize(1024),
		// 间隔多久提交, 即使未达到chunkSize
		kq.WithFlushInterval(time.Second),
		// 自动创建topic
		kq.WithAllowAutoTopicCreation(),
		// 默认的负载均衡策略
		kq.WithBalancer(&kafka.LeastBytes{}),
	)
	return &ServiceContext{
		Config:       c,
		TimeFormat:   TIME_FORMAT,
		KafkaPushCli: pusher,
	}
}
